package com.next

import grails.transaction.Transactional
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

@CompileStatic
@TypeChecked
class OpportunityService
{

    static scope = "singleton"
    OpportunityTeamService opportunityTeamService
    WorkflowInstanceService workflowInstanceService

    @Transactional
    def initOpportunity(Opportunity opportunity)
    {
        Company company = Company.find("from Company")
        // Boolean enableTerritory = company?.enableTerritory
        Boolean enableCustomizedOpptStage = company?.enableCustomizedOpptStage
        Boolean enableOpportunityWorkflow = company?.enableOpportunityWorkflow

        Territory territory
        def productOpportunityStage
        def workflow
        def workflowInstance

        if (enableCustomizedOpptStage)
        {
            productOpportunityStage = ProductOpportunityStage.find("from ProductOpportunityStage where product.id = ?", [opportunity?.product?.id])
            opportunity.stage = productOpportunityStage?.stage
            opportunity.save()
        }

        // if (enableTerritory)
        // {
        //     territory = Territory.find("from Territory where account.id = ? and product.id = ?", [opportunity?.account?.id, opportunity?.product?.id])
        //     if (territory)
        //     {
        //         opportunityTeamService.initTeam(opportunity, territory)
        //     }
        // }
        //
        // if (enableTerritory && !enableCustomizedOpptStage && enableOpportunityWorkflow)
        // {
        //     workflow = territory?.workflow
        //     if (workflow)
        //     {
        //         workflowInstance = new WorkflowInstance()
        //         workflowInstance.name = workflow?.name
        //         workflowInstance.opportunity = opportunity
        //         workflowInstance.workflow = workflow
        //         workflowInstance.save()
        //
        //         workflowInstanceService.initWorkflowInstance(workflowInstance)
        //     }
        // }
    }
}
